<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3 0003
 * Time: 21:14
 */

namespace app\admin\controller;

use app\index\model\User as UserModel;
use app\index\model\UserInfo;
use think\Controller;
class AdminUser extends Controller
{
    //中间件登录限制
    protected $middleware = [
        'AdminCheck'
    ];
    //用户列表
    public function user_list(){
        $user=new UserModel();
        $sid=input('get.username');
        if (isset($sid)) {
           $user=new UserModel();
           $user=$user->user_search($sid);
        }else{
           $user=$user->with('userinfo')->paginate('10');
        }
        $this->assign('users',$user);
        return $this->fetch();
    }
    //用户删除
    public function user_dele(){
        $id=input('post.id');
        if (isset($id)){
            $user=UserModel::get($id,'userinfo');
            $userone=$user->together('userinfo')->delete();
//            $userinfo=new UserInfo();
//            $userinfo=$userinfo->where('user_id',$id)->delete();
            if ($userone){
                $msg=$this->msg(200,'删除成功！');
            }else{
                $msg=$this->msg(400,'删除失败！');
            }
            return $msg;
        }else{
            $this->error('请求失败');
        }
    }
    //用户添加
    public function user_add(){
        if ($this->request->isAjax()){
            $data=input('post.');
            $useradd=new UserModel();
            $user=$useradd->user_add($data);
            if ($user){
                $useradd->userinfo()->save(['sid'=>$data['sid']]);
                $msg=[
                    'statu'=>200,
                    'msg'  =>'添加成功'
                ];
            }else{
                $this->msg(400,'添加失败');
            }
            return $msg;
        }
        return $this->fetch();
    }
    //返回信息
    public function msg($statu,$msg){
        return [
            'statu'=>$statu,
            'msg'  =>$msg
        ];
    } 
}